<?php

class LatestSidebarPanel
{
    const SIDEBAR_PANELS_TEMPLATES_PATH = "custom_sidebar/"; 
    
    private static function Generate( $template, TemplateEngine $tmpl )
    {
        $generatedContent = "";
        $template = self::SIDEBAR_PANELS_TEMPLATES_PATH . $template;
        
        $blogLinks = self::FetchBlogLinks();
        if ($blogLinks !== false)
        {
            $markers = array(
                "blogLinks" => $blogLinks 
            );
            $generatedContent = TemplateEngine::getView($template, $markers); 
        }       
        return $generatedContent;
    }
    
    public static function GetContent( $panelInfo, TemplateEngine $tmpl, $pageId )
    {
        $cacheId = "html/_sidebar_panels/". $panelInfo['id'];
        $content = Core::Cache()->Get($cacheId);
        if ( empty($content) )
        {
            $content = self::Generate( $panelInfo['template'], $tmpl );
            Core::Cache()->Set($cacheId, $content);
        }
        return $content;
    }
    
    /**
     * Gets blog links from blog links cache
     * @return mixed 
     * An associative array of blog links and titles represented 
     * in following format [link] => [title]
     * Boolean false on failure  
     */
    private static function FetchBlogLinks()
    {
        $result = false;
        $content = @file_get_contents(SITE_LOCAL_PATH . BLOG_LINKS_CACHE_PATH);
        if ( $content !== false )
        {
            $result = json_decode($content, true);
            $result = ($result !== NULL) ? $result : false;
        }
        return $result;
    }       
}

?>